package com.parkinglot.core;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
@Data
public class Result<T> implements Serializable {
    @ApiModelProperty("编码")
    private String code;

    @ApiModelProperty("是否成功")
    private boolean success;

    @ApiModelProperty("提示")
    private String msg;

    private T data;

    public Result(){
    }

    private Result(String code, boolean success, String msg, T data){
        this.code = code;
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    private Result(String code, boolean success, String msg){
        this.code = code;
        this.success = success;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * 设置数据
     * @param data 数据
     */
    public void setData(T data){
        this.data = data;
    }

    /**
     * 获取数据
     * @return T
     */
    public T getData(){
        return data;
    }

    private static <T> Result<T> build(ResultEnum resultEnum){
        return new Result<T>(resultEnum.getCode(), resultEnum.getSuccess(), resultEnum.getMsg());
    }

    private static <T> Result<T> build(T data){
        Result<T> result = build(ResultEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 成功
     * @return Result
     */
    public static <T> Result<T> success(){
        return build(ResultEnum.SUCCESS);
    }

    /**
     * 成功
     * @param data 返回数据
     * @return Result
     */
    public static <T> Result<T> success(T data){
        return build(data);
    }

    /**
     * 失败
     * @return Result
     */
    public static <T> Result<T> failure(){
        return build(ResultEnum.FAILURE);
    }

    /**
     * 失败
     * @param resultEnum 错误信息
     * @return Result
     */
    public static <T> Result<T> failure(ResultEnum resultEnum){
        return build(resultEnum);
    }

    /**
     * 失败
     * @param msg 错误信息
     * @return Result
     */
    public static <T> Result<T> failure(String msg){
        return new Result<T>(ResultEnum.FAILURE.getCode(), ResultEnum.FAILURE.getSuccess(), msg);
    }

    /**
     * 失败
     * @param msg 错误信息
     * @return Result
     */
    public static <T> Result<T> failure(String code, String msg){
        return new Result<T>(code, ResultEnum.FAILURE.getSuccess(), msg);
    }
}
